/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package Data;

import java.sql.Date;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JOptionPane;

/**
 *
 * @author NGOCLINH-PC
 */
public class Comment {

    private int CommentID;
    private int MemberID;
    private int EventID;
    private String Datecomment;
    private String Contents;
    private int Status;

    public Comment() {
    }

    public Comment(int CommentID, int MemberID, int EventID, String Datecomment, String Contents, int Status) {
        this.CommentID = CommentID;
        this.MemberID = MemberID;
        this.EventID = EventID;
        this.Datecomment = Datecomment;
        this.Contents = Contents;
        this.Status = Status;
    }

    public int getCommentID() {
        return CommentID;
    }

    public void setCommentID(int CommentID) {
        this.CommentID = CommentID;
    }

    public int getMemberID() {
        return MemberID;
    }

    public void setMemberID(int MemberID) {
        this.MemberID = MemberID;
    }

    public int getEventID() {
        return EventID;
    }

    public void setEventID(int EventID) {
        this.EventID = EventID;
    }

    public String getDatecomment() {
        return Datecomment;
    }

    public void setDatecomment(String Datecomment) {
        this.Datecomment = Datecomment;
    }

    public String getContents() {
        return Contents;
    }

    public void setContents(String Contents) {
        this.Contents = Contents;
    }

    public int getStatus() {
        return Status;
    }

    public void setStatus(int Status) {
        this.Status = Status;
    }

    public ArrayList<Comment> Cmtselect() {

        try {
            ArrayList<Comment> listCmt = new ArrayList<Comment>();
            Statement st = Connect.Connect.getCon().createStatement();
            ResultSet rs = st.executeQuery("SELECT * FROM Comment");
            while (rs.next()) {
                Comment cmt = new Comment(rs.getInt(1), rs.getInt(2), rs.getInt(3), rs.getString(4), rs.getString(5), rs.getInt(6));
                listCmt.add(cmt);
            }
            return listCmt;
        } catch (ClassNotFoundException ex) {
            Logger.getLogger(Category.class.getName()).log(Level.SEVERE, null, ex);
        } catch (SQLException ex) {
            Logger.getLogger(Category.class.getName()).log(Level.SEVERE, null, ex);
        }
        return null;
    }

    public ArrayList<Comment> CmtselectByID(int id) {

        try {
            ArrayList<Comment> listCmt = new ArrayList<Comment>();
            Statement st = Connect.Connect.getCon().createStatement();
            ResultSet rs = st.executeQuery("SELECT * FROM Comment where CommentID='" + id + "'");
            while (rs.next()) {
                Comment cmt = new Comment(rs.getInt(1), rs.getInt(2), rs.getInt(3), rs.getString(4), rs.getString(5), rs.getInt(6));
                listCmt.add(cmt);
            }
            return listCmt;
        } catch (ClassNotFoundException ex) {
            Logger.getLogger(Category.class.getName()).log(Level.SEVERE, null, ex);
        } catch (SQLException ex) {
            Logger.getLogger(Category.class.getName()).log(Level.SEVERE, null, ex);
        }
        return null;
    }
    public ArrayList<Comment> CmtselectByEventID(int id) {

        try {
            ArrayList<Comment> listCmt = new ArrayList<Comment>();
            Statement st = Connect.Connect.getCon().createStatement();
            ResultSet rs = st.executeQuery("SELECT * FROM Comment where EventID='" + id + "'");
            while (rs.next()) {
                Comment cmt = new Comment(rs.getInt(1), rs.getInt(2), rs.getInt(3), rs.getString(4), rs.getString(5), rs.getInt(6));
                listCmt.add(cmt);
            }
            return listCmt;
        } catch (ClassNotFoundException ex) {
            Logger.getLogger(Category.class.getName()).log(Level.SEVERE, null, ex);
        } catch (SQLException ex) {
            Logger.getLogger(Category.class.getName()).log(Level.SEVERE, null, ex);
        }
        return null;
    }

    public static void Cmtinsert(int MemberID, int EventID, String DateCmt, String contents, int Status) {
        try {
            PreparedStatement st = Connect.Connect.getCon().prepareStatement("insert into Comment values(?,?,?,?,?)");
            st.setInt(1, MemberID);
            st.setInt(2, EventID);
            st.setString(3, DateCmt);
            st.setString(4, contents);
            st.setInt(5, Status);
            ResultSet rs = st.executeQuery();
            if (rs.next()) {
                JOptionPane.showMessageDialog(null, "Insert Successful!");
            } else {
                JOptionPane.showMessageDialog(null, "Error!");
            }
        } catch (ClassNotFoundException ex) {
            Logger.getLogger(Category.class.getName()).log(Level.SEVERE, null, ex);
        } catch (SQLException ex) {
            Logger.getLogger(Category.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    public static void Cmtupdate(int CommentID, String contents, int Status) {
        try {
            PreparedStatement st = Connect.Connect.getCon().prepareStatement("update Comment SET Contents=?,Status=? WHERE CommentID=?");
            st.setString(1, contents);
            st.setInt(2, Status);
            st.setInt(3, CommentID);
            ResultSet rs = st.executeQuery();
            if (rs.next()) {
                JOptionPane.showMessageDialog(null, "Update Successful!");
            } else {
                JOptionPane.showMessageDialog(null, "Error!");
            }
        } catch (ClassNotFoundException ex) {
            Logger.getLogger(Category.class.getName()).log(Level.SEVERE, null, ex);
        } catch (SQLException ex) {
            Logger.getLogger(Category.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    public static void Cmtdelete(int CateID) {
        try {
            PreparedStatement st = Connect.Connect.getCon().prepareStatement("DELETE FROM Comment WHERE CommentID=?");
            st.setInt(1, CateID);
            ResultSet rs = st.executeQuery();
            if (rs.next()) {
                JOptionPane.showMessageDialog(null, "DELETE Successful!");
                //response.sendRedirect("DangKi.jsp");
            } else {
                JOptionPane.showMessageDialog(null, "Error!");
            }
        } catch (ClassNotFoundException ex) {
            Logger.getLogger(Category.class.getName()).log(Level.SEVERE, null, ex);
        } catch (SQLException ex) {
            Logger.getLogger(Category.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
}
